package com.zygh.policeasset.controller;

import cn.hutool.core.util.StrUtil;
import com.zygh.policeasset.component.CommonResult;
import com.zygh.policeasset.mapper.CompleteMapper;
import com.zygh.policeasset.pojo.Settlement;
import com.zygh.policeasset.service.SettlementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping({"/settlement"})
@Api(tags = {"审计结算模块"})
@RestController
public class SettlementController {
    @Autowired
    SettlementService settlementService;
    @Autowired
    CompleteMapper completeMapper;

    @RequestMapping(value = {"/getproject"}, method = {RequestMethod.GET})
    @ApiOperation("获取可添加项目")
    public Object getProject(@RequestHeader("findAll") boolean findAll,
                             @RequestHeader("userId") Integer userId) {
        if (findAll) {
            return this.settlementService.getProject(null);
        }
        return this.settlementService.getProject(userId);
    }

    @RequestMapping(value = {"/getadd"}, method = {RequestMethod.GET})
    @ApiOperation("获取可添加审计结算")
    public Object getAdd(@RequestParam String projectNo,
                         @RequestHeader("findAll") boolean findAll,
                         @RequestHeader("userId") Integer userId) {
        if (StrUtil.isEmpty(projectNo)) {
            return new CommonResult().validateFailed("项目编号不能为空");
        }
        if (findAll) {
            return this.settlementService.getAdd(projectNo, null);
        }
        return this.settlementService.getAdd(projectNo, userId);
    }

    @RequestMapping(value = {"/add"}, method = {RequestMethod.POST})
    @ApiOperation("添加")
    public Object add(@RequestBody Settlement settlement) throws ParseException {
        if (StrUtil.isEmpty(settlement.getProjectNo())) {
            return new CommonResult().validateFailed("项目ID不能为空");
        }
        if (StrUtil.isEmpty(settlement.getProjectName())) {
            return new CommonResult().validateFailed("项目名称不能为空");
        }
        if (StrUtil.isEmpty(settlement.getSectionId())) {
            return new CommonResult().validateFailed("标段id不能为空");
        }
        String finalTime = this.completeMapper.getfinalTime(settlement.getProjectNo(), Integer.valueOf(Integer.parseInt(settlement.getSectionId())));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if (sdf.parse(settlement.getAuditTime()).before(sdf.parse(finalTime))) {
            return new CommonResult().validateFailed("结算审计时间不能早于竣工验收时间");
        }
        if (this.settlementService.add(settlement) > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("添加错误");
    }

    @RequestMapping(value = {"/"}, method = {RequestMethod.GET})
    @ApiOperation("查询所有")
    public Object findAll(@RequestParam(required = false) String name,
                          @RequestParam(value = "status", required = false) Integer status,
                          @RequestParam(value = "companyId", required = false) Integer companyId,
                          @RequestParam(defaultValue = "1", value = "pageNum", required = false) Integer pageNum,
                          @RequestParam(defaultValue = "10", value = "pageSize", required = false) Integer pageSize,
                          @RequestParam(required = false) String time1,
                          @RequestParam(required = false) String time2,
                          @RequestParam(required = false) Integer category,
                          @RequestHeader("findAll") boolean findAll,
                          @RequestHeader("userId") Integer userId) {
        if (findAll) {
            return new CommonResult().success(this.settlementService.findAll(name, status, companyId, pageNum, pageSize, time1, time2, category, null));
        }
        return new CommonResult().success(this.settlementService.findAll(name, status, companyId, pageNum, pageSize, time1, time2, category, userId));
    }

    @RequestMapping(value = {"/status"}, method = {RequestMethod.GET})
    @ApiOperation("根据状态获取分页列表")
    public Object getByStatus(@RequestParam int status, @RequestParam(defaultValue = "1", value = "pageNum") int pageNum, @RequestParam(defaultValue = "10", value = "pageSize") int pageSize) {
        if (StrUtil.isEmpty(String.valueOf(status))) {
            return new CommonResult().validateFailed("状态不能为空");
        }
        return this.settlementService.findByStatus(status, pageNum, pageSize);
    }

    @RequestMapping(value = {"/delete"}, method = {RequestMethod.GET})
    @ApiOperation("删除")
    public Object delete(@RequestParam Integer id) {
        if (StrUtil.isEmpty(String.valueOf(id))) {
            return new CommonResult().validateFailed("id不能为空");
        }
        if (this.settlementService.delete(id) > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("删除失败");
    }

    @RequestMapping(value = {"/selectbyid"}, method = {RequestMethod.GET})
    @ApiOperation("通过ID查询")
    public Object selectById(Integer id) {
        if (StrUtil.isEmpty(String.valueOf(id))) {
            return new CommonResult().validateFailed("id不能为空");
        }
        return new CommonResult().success(this.settlementService.selectById(id));
    }

    @RequestMapping(value = {"/update"}, method = {RequestMethod.POST})
    @ApiOperation("更新")
    public Object update(@RequestBody Settlement settlement) throws ParseException {
        if (StrUtil.isEmpty(settlement.getProjectNo())) {
            return new CommonResult().validateFailed("项目编号不能为空");
        }
        if (StrUtil.isEmpty(settlement.getAuditTime())) {
            return new CommonResult().validateFailed("审计结算日期不能为空");
        }
        String finalTime = this.completeMapper.getfinalTime(settlement.getProjectNo(), Integer.valueOf(Integer.parseInt(settlement.getSectionId())));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if (sdf.parse(settlement.getAuditTime()).before(sdf.parse(finalTime))) {
            return new CommonResult().validateFailed("结算审计时间不能早于竣工验收时间");
        }
        if (this.settlementService.update(settlement) > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("更新失败");
    }
}